package com.flying.pas.security.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.common.util.MD5;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.pas.security.model.Role;
import com.flying.pas.security.model.User;

@Service(value="UserService", desc="")
public class UserService extends AbstractService{
	private RoleUserService rus;
	private OrgService os;
	
	public void setRoleUserService(RoleUserService s) {
		this.rus = s;
	}
	
	public void setOrgService(OrgService s) {
		this.os = s;
	}
	
	@MethodInfo("新增")
	@DaoCreate(entity=User.ENTITY_NAME)
	public User create(
			@Param(value=User.USERNAME, required=true,	desc="登录名") String username,
			@Param(value=User.PASSWORD, required=false,	desc="密码") String password,
			@Param(value=User.MOBILE, required=false,	desc="手机") String mobile,
			@Param(value=User.MAIL, required=false,	desc="邮箱") String mail,
			
			@Param(value=User.ORG_ID, required=false,	desc="组织机构ID") Long org_id,
			@Param(value=User.ORG_CODE, required=false,	desc="组织机构代码") String org_code,
			@Param(value=User.REAL_NAME, required=false,	desc="实名") String real_name,
			@Param(value=User.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=User.CREATE_DATE, required=false,	desc="创建时间") Date create_date,
			@Param(value=User.CREATOR, required=false,	desc="创建人") String creator
		
		) throws Exception {
		if(org_id != null) {
			org_code = this.os.findById(org_id).getCode();
		}
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(User.class, 
				User.PASSWORD, MD5.encode(password == null?"123456": password),
				User.ORG_CODE, org_code,
				User.CREATOR, p == null?null: p.getName(),
				User.CREATE_DATE, new Date());
	}
    
	@MethodInfo("新增")
	@Transaction
	public User createWithRoles(
			@Param(value=User.USERNAME, required=true,	desc="登录名") String username,
			@Param(value=User.PASSWORD, required=false,	desc="密码") String password,
			@Param(value=User.MOBILE, required=false,	desc="手机") String mobile,
			@Param(value=User.MAIL, required=false,	desc="邮箱") String mail,
			@Param(value=User.ORG_ID, required=false,	desc="组织机构") Long org_id,
			@Param(value=User.REAL_NAME, required=false,	desc="实名") String real_name,
			@Param(value=User.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value="roleIds", required=true,	desc="角色IDs") Long[] role_ids,
			@Param(value=User.CREATE_DATE, required=false,	desc="创建时间") Date create_date,
			@Param(value=User.CREATOR, required=false,	desc="创建人") String creator
		
		) throws Exception {
		User user = this.create(username, password, mobile, mail, 
				org_id, null, real_name, remarks, create_date, creator);
		this.rus.updateUserRoles(user.getUserId(), role_ids);
		return user;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=User.ENTITY_NAME)
	public ActionResult update(
			@Param(value=User.USER_ID, required=true,	desc="用户ID") Long user_id,
			@Param(value=User.MOBILE, required=false,	desc="手机") String mobile,
			@Param(value=User.MAIL, required=false,	desc="邮箱") String mail,
			@Param(value=User.ORG_ID, required=false,	desc="组织机构") Long org_id,
			@Param(value=User.ORG_CODE, required=false,	desc="组织机构代码") String org_code,
			@Param(value=User.REAL_NAME, required=false,	desc="实名") String real_name,
			@Param(value=User.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=User.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=User.UPDATE_DATE, required=false,	desc="修改时间") Date update_date
			
		) throws Exception {
		if(org_id != null) {
			org_code = this.os.findById(org_id).getCode();
		}
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				User.ORG_CODE, org_code,
				User.UPDATOR, p == null?null:p.getName(),
				User.UPDATE_DATE, new Date());
	}

	@MethodInfo("修改")
	@Transaction
	public ActionResult updateWithRoles(
			@Param(value=User.USER_ID, required=true,	desc="用户ID") Long user_id,
			@Param(value=User.MOBILE, required=false,	desc="手机") String mobile,
			@Param(value=User.MAIL, required=false,	desc="邮箱") String mail,
			@Param(value=User.ORG_ID, required=false,	desc="组织机构") Long org_id,
			@Param(value=User.REAL_NAME, required=false,	desc="实名") String real_name,
			@Param(value=User.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value="roleIds", required=true,	desc="角色IDs") Long[] role_ids,
			@Param(value=User.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=User.UPDATE_DATE, required=false,	desc="修改时间") Date update_date
			
		) throws Exception {
		this.rus.updateUserRoles(user_id, role_ids);
		return this.update(user_id, mobile, mail, org_id, null, real_name, remarks, updator, update_date);
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=User.ENTITY_NAME)
	public ActionResult updatePassword(
			@Param(value=User.USER_ID, required=true,	desc="用户ID") Long user_id,
			@Param(value=User.PASSWORD, required=true,	desc="密码") String password,
			@Param(value=User.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=User.UPDATE_DATE, required=false,	desc="修改时间") Date update_date
			
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				User.PASSWORD, MD5.encode(password),
				User.UPDATOR, p.getName(),
				User.UPDATE_DATE, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=User.ENTITY_NAME)
	public ActionResult updateOrgCode(
			@Param(value=User.ORG_ID, required=false,	desc="组织机构", tag="=") Long org_id,
			@Param(value=User.ORG_CODE, required=false,	desc="组织机构代码") String org_code
		) throws Exception {
		return null;
	}

	@MethodInfo("删除")
	@Transaction
	@DaoRemove(entity=User.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=User.USER_ID, required=true,	desc="用户ID") Long user_id
		) throws Exception {
		this.rus.removeByUser(user_id);
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=User.ENTITY_NAME, pageable=false, modelClass=User.class)
	public QueryResult<User> findByOrgAndRole(
			@Param(value=User.ORG_CODE, required=true,	desc="组织机构代码") String org_code,
			@Param(value=Role.ROLE_CODE, required=true, desc="角色代码") String role_code) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=User.ENTITY_NAME, pageable=true, modelClass=User.class)
	public QueryResult<User> findAll(
			@Param(value=User.ORG_ID, required=false,	desc="组织机构") Long org_id,
			@Param(value=User.USERNAME, required=false,	desc="登录名", tag="like") String username,
			@Param(value=User.REAL_NAME, required=false,	desc="实名", tag="like") String real_name,
			@Param(value=User.MOBILE, required=false,	desc="手机") String mobile,
			@Param(value=User.MAIL, required=false,	desc="邮箱") String mail,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=User.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public User findById(
			@Param(value=User.USER_ID, required=true,	desc="用户ID") Long user_id
		) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=User.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public User findByUsernamePassword(
			@Param(value=User.USERNAME, required=true,	desc="登录名") String username,
			@Param(value=User.PASSWORD, required=true,	desc="密码") String password
		) throws Exception {
		return ModelFactory.create(User.class, 
				User.PASSWORD, MD5.encode(password));
	}

	@MethodInfo("查询")
	public User login(
			@Param(value=User.USERNAME, required=true,	desc="登录名") String username,
			@Param(value=User.PASSWORD, required=true,	desc="密码") String password
		) throws Exception {
		return ModelFactory.create(User.class, 
				User.PASSWORD, MD5.encode(password));
	}

}